
import {notification} from 'antd'
import axios from 'axios'
import { useEffect, useState } from 'react'
function usePublish(type) {
    const handlePublish = (id) => {

        setDataSource(dataSource.filter(item => item.id !== id))
        axios.patch("/news/" + id, {
            "publishState": 2,

        }).then(res => {
            const args = {
                message: 'Notification Title',
                description: `你可以到发布管理/已发布中查看你的新闻`,
                duration: 3,
            };
            notification.open(args);
        })
    } 
    // 0.001685
    const handleSunset = (id) => {
        setDataSource(dataSource.filter(item => item.id !== id))
        axios.patch("/news/" + id, {
            "publishState": 3,

        }).then(res => {
            const args = {
                message: 'Notification Title',
                description: `你可以到发布管理/已下线中查看你的新闻`,
                duration: 3,
            };
            notification.open(args);
        })
    }
    const handleDelete = (id) => {
        setDataSource(dataSource.filter(item => item.id !== id))
        axios.delete(`/news/${id}`).then(res => {
            const args = {
                message: 'Notification Title',
                description: `您已删除成功`,
                duration: 3,
            };
            notification.open(args);
        })
    }
    const { username } = JSON.parse(localStorage.getItem("token"))
    const [dataSource, setDataSource] = useState([])
    useEffect(() => {
        axios.get(`/news?author=${username}&publishState=${type}&_expand=category`).then(res => {
            setDataSource(res.data)
        })


    }, [])
    return {
        dataSource,
        handlePublish,
        handleSunset,
        handleDelete,
    }
}

export default usePublish
